package com.goldenKey.common.component;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.goldenKey.common.constant.Constant;
import com.goldenKey.common.exception.AppException;
import com.goldenKey.common.utils.RedisUtils;
import com.goldenKey.common.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;


/**
 * 登陆操作的辅助方法
 * type 0:管理员；1：用户
 */

@Slf4j
@Component
public class AccountComponent {
    private static final String X_FORWARDED_FOR = "X-Forwarded-For";
    private static final String PROXY_CLIENT_IP = "Proxy-Client-IP";
    private static final String WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";

    // 引进邮件接口
    @Autowired
    private JavaMailSender mailSender;
    // 获得发件人信息
    @Value("${spring.mail.username}")
    private String from;


    @Resource
    private RedisComponent redisComponent;

    /**
     * 用户注册时,发送邮箱验证码
     * @param toEmail
     */
    public void sendCommonEmail(HttpSession session, String toEmail) {
        // 获取随机验证码
        String verCode = StringTools.getRandomString(6);

        // 创建邮件信息
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(toEmail);
        message.setSubject("验证码:" + verCode);

        message.setText("金玥智阅:\n" +
                "\t\t尊敬的读者,您好!\n"
                + "\t\t由于用户注册或密码修改请求的邮件验证码为:" + verCode + ",本验证码 2 分钟内效，请及时输入。（请勿泄露此验证码）\n"
                + "\n　　如非本人操作，请忽略该邮件。\n　　(这是一封通过自动发送的邮件，请不要直接回复）");

        try {
            // 发送邮件
            mailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
            throw new AppException("发送验证码邮件失败！");
        }

        // 获取邮箱验证码和当前时间戳并存入 session
        Long timestamp = System.currentTimeMillis();
        session.setAttribute(Constant.EMAIL_CODE_TIMESTAMP_KEY, timestamp);
        session.setAttribute(Constant.EMAIL_CODE_KEY, verCode);
    }





    /**
     * 获取用户登陆的IP
     */
    public String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader(X_FORWARDED_FOR);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader(PROXY_CLIENT_IP);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader(WL_PROXY_CLIENT_IP);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }




    public static void main(String[] args) {
        System.out.println();
    }
}
